﻿package com.bizmlib.utils
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	
	[Event(name="change", type="flash.events.Event")]
	
	
	public class CommonPropertyController extends EventDispatcher
	{
		public static const TYPE_MIN:Number = 0;
		public static const TYPE_MAX:Number = 1;
		public static const TYPE_NUMMIN:Number = 2;
		public static const TYPE_NUMMAX:Number = 3;
		
		private var __type:Number;
		private var __propertyName:String;
		private var __propertyValue:*;
		private var lastPropertyValue:*;
		private var __locked:Boolean;
		private var values:Dictionary;
		
		
		public function CommonPropertyController(type:Number = undefined, propertyName:String = "")
		{
			values = new Dictionary(true);
			__propertyName = propertyName;
			locked = false;
			this.type = isNaN(type) ? TYPE_MIN : type;
		}
		
		
		public function set type(value:Number):void
		{
			__type = value;
			checkPropertyValue();
		}
		public function get type():Number
		{
			return __type;
		}
		public function set locked(value:Boolean):void
		{
			__locked = value;
			
			if(locked)
			{
				lastPropertyValue = propertyValue;
			}
			else
			{
				checkPropertyValue();
			}
		}
		public function get locked():Boolean
		{
			return __locked;
		}
		
		public function get propertyValue():*
		{
			return __propertyValue;
		}
		public function get propertyName():String
		{
			return __propertyName;
		}
		
		
		public function setValue(key:String, value:*):void
		{
			//trace(this + ".setValue(" + arguments + ")");
			
			values[key] = value;
			
			checkPropertyValue();
		}
		public function removeValue(key:String):void
		{
			if(values[key])
			{
				delete values[key];
			}
			
			checkPropertyValue();
		}
		public function clean():void
		{
			values = null;
		}
		
		
		protected function checkPropertyValue():void
		{
			if(locked)
			{
				return;
			}
			
			var valuesArr:Array = new Array();
			for(var key:* in values)
			{
				valuesArr.push(values[key]);
			}
			
			var sortParameters:Array = new Array();
			switch(type)
			{
				case TYPE_MIN:
					break;
				case TYPE_MAX:
					sortParameters.push(Array.DESCENDING);
					break;
				case TYPE_NUMMIN:
					sortParameters.push(Array.NUMERIC);
					break;
				case TYPE_NUMMAX:
					sortParameters.push(Array.DESCENDING | Array.NUMERIC);
					break;
				default:
					sortParameters = null;
					break;
			}
			
			var newPropertyValue:*;
			if(sortParameters)
			{
				valuesArr.sort.apply(valuesArr, sortParameters);
				newPropertyValue = (valuesArr.length > 0) ? valuesArr[0] : undefined;
			}
			else
			{
			}
			
			if(newPropertyValue != lastPropertyValue)
			{
				lastPropertyValue = newPropertyValue;
				__propertyValue = newPropertyValue;
				dispatchChangeEvent();
			}
		}
		protected function dispatchChangeEvent():void
		{
			dispatchEvent(new Event(Event.CHANGE, false, true));
		}
		
		
		override public function toString():String
		{
			return "[CommonPropertyController	propertyName=" + propertyName + "	propertyValue:" + propertyValue + "]";
		}
	}
}